#include "LinkedList.hpp"

template<class T>
LinkedList<T>::LinkedList(){

}

template<class T>
LinkedList<T>::LinkedList(const std::vector<T> & values){

    if(values.size() == 0){
        return;
    }
    Node<T> * head = new Node<T>(values[0]);
    Node<T> * p = head;

    for(int i=1; i< values.size(); i++,p = p->next){
        Node<T> * node = new Node<T>(values[i]);
        p->next = node;
    }

    this->head = head;
}

template<class T>
LinkedList<T>::~LinkedList(){

}

template<class T>
void LinkedList<T>::add_tail(const T &value){
    Node<T> * node = new Node<T>(value);

    if(this->head == NULL){
        this->head = node;
    }
    else{
        Node<T> * p = this->head;
        while(p->next != NULL){
             p = p ->next;  
        }        
        
        //使用尾插法
        p->next = node;
    }

    return;
}

template<class T>
void LinkedList<T>::add_front(const T &value){
    Node<T> * node = new Node<T>(value);
    if(this->head == NULL){
        this->head = node;
    }
    else{
       //使用头插法
       node->next = this->head;
       this->head = node; 
    }
    return;
}

template<class T>
void LinkedList<T>::view(){
    Node<T> * p = this->head;
    
    vector<T> res;
    while(p != NULL) {
        res.push_back(p->data);
        p = p->next;
    }
    fmt::print("{}\n", res);
}

template<class T>
void LinkedList<T>::reverse(){
    Node<T>* post = this->head->next;
    Node<T>* pre = NULL;
    
    while(post != NULL){
        this->head->next = pre;

        pre = this->head;
        this->head = post;
        post = post->next;
    }

    this->head = pre;
}